﻿
@{
    ViewBag.Title = "打包详情";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var workshops = ViewBag.WorkShops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
    var user = ViewBag.User as IOA.MES.DataAccess.VModel.VUser;
}
@model IOA.MES.DataAccess.VModel.VPack

@section Contents{
    <style>
        .layui-form-label {
            width: auto;
        }

        .layui-table-cell {
            height: auto;
        }

        .grid-demo {
            line-height: 40px;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.Operator}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" value="@(Model==null?user.TrueName:Model.CNCreateBy)" disabled class="layui-input" @(Model != null ? "disabled" : "")>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.WorkShop}}：</label>
                        <div class="layui-input-inline">
                            @if (Model == null)
                            {
                                <select id="selWorkshops" name="WorkshopCode" lay-filter="selWorkshops" @(Model != null ? "disabled" : "")>
                                    @foreach (var item in workshops)
                                    {
                                        <option value="@item.WorkshopCode">@item.WorkshopName</option>
                                    }
                                </select>
                            }
                            else
                            {
                                <input type="text" value="@Model.WorkshopName" disabled class="layui-input">
                            }
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.Duty}}：</label>
                        <div class="layui-input-inline">
                            @if (Model == null)
                            {
                                <select id="selDuties" name="DutyId" lay-filter="selDuties" @(Model != null ? "disabled" : "")>
                                    <option value="0">选择班次</option>
                                </select>
                            }
                            else
                            {
                                <input type="text" value="@Model.DutyName" disabled class="layui-input">
                            }
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.Machine}}：</label>
                        <div class="layui-input-inline">
                            @if (Model == null)
                            {
                                <select id="selMachines" name="MachineId" lay-filter="selMachines" @(Model != null ? "disabled" : "")>
                                    <option value="0">选择机台</option>
                                </select>
                            }
                            else
                            {
                                <input type="text" value="@Model.MachineName " disabled class="layui-input">
                            }
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.PackBatchNo}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="PackBatchNo" placeholder="请输入包装批次号" autocomplete="off" class="layui-input" value="@(Model?.PackBatchNo )" @(Model != null ? "disabled" : "")>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.PackNo}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="PackNo" placeholder="请输入包装编码" autocomplete="off" class="layui-input" value="@(Model?.PackNo )" @(Model != null ? "disabled" : "")>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <input type="hidden" name="PkId" value=" @(Model==null?0:Model.PkId)" />
                        @if (Model != null && Model.PkId > 0 && Model.PackStatus == IOA.MES.DataAccess.Enum.PackStatus.Normal && Model.PackInspectStatus == IOA.MES.DataAccess.Enum.PackInboundInspectStatus.Inspected)
                        {
                            <button class="layui-btn layui-btn-danger layuiadmin-btn-list" name="UnPack" lay-filter="submit" lay-submit type="submit">{{lang.Field.UnPack}}</button>
                        }
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card layui-form">
            <div class="layui-card-header">
                {{lang.Field.PackParams}}
            </div>
            <div class="layui-card-body packconditions">
                <div class="layui-row">
                    <div class="layui-col-xs3">
                        <input type="checkbox" name="MaxSize" lay-filter="MaxSize" lay-skin="primary" title="打包数量" disabled>
                    </div>
                    <div class="layui-col-xs3">
                        <input type="checkbox" name="OrderNoLimit" lay-filter="OrderNoLimit" lay-skin="primary" title="订单号" disabled>
                    </div>
                    <div class="layui-col-xs3">
                        <input type="checkbox" name="WorkOrderNoLimit" lay-filter="WorkOrderNoLimit" lay-skin="primary" title="工单号" disabled>
                    </div>
                    <div class="layui-col-xs3">
                        <input type="checkbox" name="DataPlate" lay-filter="DataPlate" lay-skin="primary" title="铭牌型号" disabled>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-xs3">
                        <input type="checkbox" name="RatedPower" lay-filter="RatedPower" lay-skin="primary" title="额定功率" disabled>
                    </div>
                    <div class="layui-col-xs3">
                        <input type="checkbox" name="ElectricGrade" lay-filter="ElectricGrade" lay-skin="primary" title="电流档" disabled>
                    </div>
                    <div class="layui-col-xs3">
                        <input type="checkbox" name="CompGrade" lay-filter="CompGrade" lay-skin="primary" title="组件等级" disabled>
                    </div>
                    <div class="layui-col-xs3">
                        <input type="checkbox" name="CompColor" lay-filter="CompColor" lay-skin="primary" title="组件颜色" disabled>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-xs3">
                        <input type="checkbox" name="JunBoxSpecs" lay-filter="JunBoxSpecs" lay-skin="primary" title="接线盒规格" disabled>
                    </div>
                    <div class="layui-col-xs3">
                        <input type="checkbox" name="JunBoxFactory" lay-filter="JunBoxFactory" lay-skin="primary" title="接线盒厂家" disabled>
                    </div>
                    <div class="layui-col-xs3">
                        <input type="checkbox" name="BackPlanSpecs" lay-filter="BackPlanSpecs" lay-skin="primary" title="背板规格" disabled>
                    </div>
                    <div class="layui-col-xs3">
                        <input type="checkbox" name="BackPlanFactory" lay-filter="BackPlanFactory" lay-skin="primary" title="背板厂家" disabled>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-xs3">
                        <input type="checkbox" name="AluFrameSpecs" lay-filter="AluFrameSpecs" lay-skin="primary" title="铝框规格" disabled>
                    </div>
                    <div class="layui-col-xs3">
                        <input type="checkbox" name="AluFrameFactory" lay-filter="AluFrameFactory" lay-skin="primary" title="铝框厂家" disabled>
                    </div>
                    <div class="layui-col-xs3">
                        <input type="checkbox" name="BatterySpecs" lay-filter="BatterySpecs" lay-skin="primary" title="电池片规格" disabled>
                    </div>
                    <div class="layui-col-xs3">
                        <input type="checkbox" name="BatteryFactory" lay-filter="BatteryFactory" lay-skin="primary" title="电池片厂家" disabled>
                    </div>
                    <div class="layui-col-xs3">
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card layui-form">
            <div class="layui-card-header">
                {{lang.Field.BasisForPackConsolidationRules}}
            </div>
            <div class="layui-card-body packconditions">
                <div class="layui-row">
                    <div class="layui-col-xs12">
                        <span id="spCondition"></span>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        var packModel = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model));

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'form', 'laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate;
                laydate.render({
                    elem: '#txtRangeDate'
                    , range: '至'
                });

                form.on('select(selWorkshops)', function (data) {
                    queryDuties();
                    queryMachines();
                });

                if (packModel == null) {
                    queryDuties();
                    queryMachines();
                }
                function queryDuties() {
                    var workshopCode = $("#selWorkshops").val().trim();
                    if (workshopCode == '') {
                        return;
                    }
                    var loadId = layer.load();
                    $.ajax({
                        url: "/Settings/QueryDuties",
                        data: {
                            page: 1,
                            limit: 0,
                            workshopCode: workshopCode,
                            status: 1
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            $("#selDuties").html("");
                            var options = new Array();
                            for (var i = 0; i < res.Data.length; i++) {
                                options.push("<option value=\"" + res.Data[i].PkId + "\">" + res.Data[i].DutyName + "</option>");
                            }
                            $("#selDuties").html(options.join(""));
                            form.render();
                        }
                    });
                }
                function queryMachines() {
                    var workshopCode = $("#selWorkshops").val().trim();
                    if (workshopCode == '') {
                        return;
                    }
                    var loadId = layer.load();
                    $.ajax({
                        url: "/Settings/QueryMachines",
                        data: {
                            page: 1,
                            limit: 0,
                            workshopCode: workshopCode,
                            lineId:0,
                            status: 1,
                            machineType:130
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            $("#selMachines").html("");
                            var options = new Array();
                            for (var i = 0; i < res.Data.length; i++) {
                                options.push("<option value=\"" + res.Data[i].PkId + "\">" + res.Data[i].MachineName + "</option>");
                            }
                            $("#selMachines").html(options.join(""));
                            form.render();
                        }
                    });
                }

                form.on('submit(submit)', function (formData) {
                    if (formData.elem.name == "UnPack") {
                        unPack();
                    }
                });
                function unPack() {
                    layer.confirm('确定要解除打包吗？', { icon: 3, title: '提示' }, function (index) {
                        var loadId = layer.load();
                        $.ajax({
                            url: "/WorkOrder/UnPack",
                            data: {
                                packNo: packModel.PackNo
                            },
                            complete: function () {
                                layer.close(loadId);
                            },
                            success: function (res) {
                                if (res.ErrCode != 200) {
                                    layer.msg(res.ErrMsg, { icon: 5 });
                                    return;
                                }
                                window.parent.layui.layer.msg("解除打包成功！");
                                location.reload();
                            }
                        });
                    });
                }

                var options = {
                    elem: '#table'
                    , id: "table"
                    , limit: 1000
                    , cols: [[ //标题栏
                        { title: '序号', width: 80, type: "numbers" }
                        , { field: 'BarCode', title: '条码' }
                        , {
                            title: '状态', templet: function (d) {
                                return d.Status == '已打包' ? "<span style='color:green;'>" + d.Status + "</span>" : "<span style='color:red;white-space: pre-wrap;'>" + d.Status + "</span>";
                            }
                        }
                    ]]
                    , data: packModel == null ? [] : packModel.Products
                    , limit: packModel == null ? 0 : packModel.Products.length
                    , even: true
                };
                if (packModel != null) {
                    $("#txtBarCode").focus();
                    renderPackCondition(packModel.PackCondition);
                    if (packModel.Products.length > 0) {
                        for (var i = 0; i < packModel.Products.length; i++) {
                            packModel.Products[i].Status = "已打包";
                        }
                    }
                }

                queryTable();
                function queryTable() {
                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }
                function renderPackCondition(condition) {
                    if (!condition) {
                        return;
                    }
                    $(".packconditions [type=checkbox]").each(function (e) {
                        for (var item in condition) {
                            if (item == $(this).attr("name")) {
                                if (item == "MaxSize") {
                                    $(this)[0].checked = condition[item] != null;
                                    $(this)[0].title = "打包数量" + (condition[item] != null ? ("：" + condition[item]) : "");
                                } else {
                                    $(this)[0].checked = condition[item];
                                }
                            }
                        }
                    });
                    $("#spCondition").text(condition.WorkOrderNo != "" ? ("工单配置：" + condition.WorkOrderNo) : ("订单配置：" + condition.OrderNo));

                    form.render("checkbox");
                }
            });
        }
    </script>
}
